/**
 * Copyright (c) 2024 Sebastian Zarnekow (http://www.itemis.eu) and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 * 
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.xtend.core.tests.compiler;

import org.eclipse.xtend2.lib.StringConcatenation;
import org.junit.Test;

/**
 * @author Sebastian - Initial contribution and API
 */
@SuppressWarnings("all")
public class CompilerBug2351Test extends AbstractXtendCompilerTest {
  @Test
  public void test_01() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package p");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.math.BigDecimal");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def Object m(int i) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val r = switch(i) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("case 1: 1.0 // double");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("case 2: 1.0bd // BigDecimal");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("default: weird(\"\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return r\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("private def <T> T weird(Object object) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return null;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package p;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import java.math.BigDecimal;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public Object m(final int i) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("Object _switchResult = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("switch (i) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 1:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = Double.valueOf(1.0);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 2:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = new BigDecimal(\"1.0\");");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("default:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = this.<Object>weird(\"\");");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("final Object r = _switchResult;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return r;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("private <T extends Object> T weird(final Object object) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return null;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }

  @Test
  public void test_02() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package p");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.math.BigDecimal");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void m() {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("val s1 = switch 45 { // int");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("case 2: 1");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("case 3: 3.0");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("case 78: exit(\'.\')");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("case 4: 4.0 / 5.0");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("default: 6");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("val s2 = switch 45 {                       // Number");
    _builder.newLine();
    _builder.append("\t\t\t        ");
    _builder.append("case 78: exit(\'.\')");
    _builder.newLine();
    _builder.append("\t\t\t        ");
    _builder.append("case 2: 1");
    _builder.newLine();
    _builder.append("\t\t\t        ");
    _builder.append("case 3: 3.0");
    _builder.newLine();
    _builder.append("\t\t\t        ");
    _builder.append("case 4: 4.0/5.0");
    _builder.newLine();
    _builder.append("\t\t\t        ");
    _builder.append("default: 6 ");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("} ");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("s1.toString");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("s2.toString");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("def <T> T exit(String message) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new UnsupportedOperationException(message)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package p;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public void m() {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("Number _switchResult = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("final int _switchValue = 45;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("switch (_switchValue) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 2:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = Integer.valueOf(1);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 3:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = Double.valueOf(3.0);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 78:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = ((Number)this.<Number>exit(\".\"));");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 4:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = Double.valueOf((4.0 / 5.0));");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("default:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult = Integer.valueOf(6);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("final Number s1 = ((Number)_switchResult);");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("Number _switchResult_1 = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("final int _switchValue_1 = 45;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("switch (_switchValue_1) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 78:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult_1 = ((Number)this.<Number>exit(\".\"));");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 2:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult_1 = Integer.valueOf(1);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 3:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult_1 = Double.valueOf(3.0);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("case 4:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult_1 = Double.valueOf((4.0 / 5.0));");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("default:");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_switchResult_1 = Integer.valueOf(6);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("break;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("final Number s2 = ((Number)_switchResult_1);");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("s1.toString();");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("s2.toString();");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public <T extends Object> T exit(final String message) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("throw new UnsupportedOperationException(message);");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }
}
